﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace TestApp.Xml
{
    public partial class XmlTestForm : Form
    {
        public Session Session;

        public XmlTestForm()
        {
            InitializeComponent();
            Session = new Session();
            Session.Name = "Test-Session";
            Session.Time = DateTime.Now;
            Session.Contacts.Add(new Contact(new Address("Clint", "Eastwood")));
            Session.Contacts.Add(new Contact(new Address("Marty", "McFly")));
            // Add cross references
            Session.Contacts[0].Friends.Add(Session.Contacts[1]);
            Session.Contacts[1].Friends.Add(Session.Contacts[0]);
            Session.Contacts[0].SpecialFriend = Session.Contacts[1];
        }

        private void buttonCreateXml_Click(object sender, EventArgs e)
        {
            XmlDocument doc = Td.Additional.Xml.Xml.CreateXmlDocument("Session");
            Td.Additional.Xml.Xml.WritePropertiesToXml(doc, Session);
            doc.Save("Session-Test.xml");
            using (StreamReader sr = new StreamReader("Session-Test.xml"))
                textBoxXmlResult.Text = sr.ReadToEnd();
        }

        private void buttonLoadXml_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Session-Test.xml");
            Session = new Session();
            Td.Additional.Xml.Xml.LoadPropertiesFromXml(doc, Session);
        }
    }
}
